/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Form.Module.Fields.ListAndViewFiles = Ext.extend(Serum.Form.Module.Fields.OneToManyPreview, {

    /**
     * Constructor
     *
     * @param   {Object}    baseConfig
     */
    constructor: function(baseConfig) {
        Serum.Form.Module.Fields.ListAndViewFiles.superclass.constructor.call(this, {
            controller:     'Fileiterator',
            action:         'getFileDetail',

            toolbar:        []
        });
    },

    /**
     * Event handler
     */
    onEditElement: function() {
        var self = this;

        var onSuccess = function() {
            win.close();
        }

        var form = new Serum.Form.Module.Fields.ListAndViewFilesEdit({}, { controller: this.controller, action: this.action, parentRoot: this.parentRoot }, onSuccess);

        var win = new Ext.Window({
            title:      this.getSelectionModel().getSelected().data.title,
            layout:     'fit',
            width:      500,
            height:     400,
            closable:   true,
            resizable:  true,
            plain:      true,
            border:     false,
            autoScroll: true,
            modal:      true,
            items:      [
                            form
                        ]
        });

        form.preload(this.getSelectionModel().getSelected().data.id);
        form.load(this.fieldsList);

        win.show();
    }

});

Serum.Form.Module.Fields.ListAndViewFilesEdit = Ext.extend(Serum.Form.Module.Edit, {

    /**
     * Constructor
     *
     * @param   {Object}    baseConfig
     */
    constructor: function(baseConfig, selector, onSuccess) {
        Serum.Form.Module.Fields.ListAndViewFilesEdit.superclass.constructor.call(this, Ext.apply({
            buttons:        [],
            keys:           []

        }, baseConfig), selector, onSuccess);
    },

    /**
     * Private build
     *
     * @param   {Object}    response    Fields list
     * @param   {Object}    fieldsList
     */
    _build: function(response, fieldsList) {
        this.add({
            xtype:  'panel',
            html:   response.fields,
            border: false
        });

        this.doLayout();
    }
});

Ext.reg('listandviewfilesfield', Serum.Form.Module.Fields.ListAndViewFiles);